﻿$(document).ready(function() {
    GridDemo.Home.Products.setupGrid($("#grid"), $("#pager"), $("#search"));
});

GridDemo.Home.Products = {
    setupGrid: function(grid, pager, search) {
        var lastsel;
        grid.jqGrid({
            url: "Products/List",
            mtype: "get",
            datatype: "json",
            colNames: ['Edit', 'Details', 'ProductID', 'ProductName', 'UnitPrice', 'Discontinued'],
            colModel: [
                        { name: 'Edit', index: 'Edit', width: 25, align: 'center', sortable: false, hidden: false },
                        { name: 'Details', index: 'Details', width: 25, align: 'center', sortable: false, hidden: false },
                        { name: 'ProductID', index: 'ProductID', width: 50, align: 'center', sortable: false, editable: true, hidden: true },
                        { name: 'ProductName', index: 'ProductName', sortable: true, editable: true, hidden: false },
                        { name: 'UnitPrice', index: 'UnitPrice', width: 40, align: 'center', formatter: 'currency', formatoptions: { prefix: "$" }, sortable: true, editable: true, hidden: false },
                        { name: 'Discontinued', index: 'Discontinued', width: 50, align: 'center', sortable: false, hidden: false }
                      ],
            rowNum: 10,
            rowList: [10, 20, 50],
            pager: pager,
            sortname: 'ProductID',
            sortorder: "asc",
            viewrecords: true,
            multiselect: false,
            onSelectRow: function(id) {
                if (id && id !== lastsel) {
                    //grid.restoreRow(lastsel);
                    grid.saveRow(lastsel, checksave);
                    grid.editRow(id, true);
                    lastsel = id;
                }
            },
            editurl: "Products/Edit",
            width: '100%',
            height: '100%',
            autowidth: true,
            rownumbers: true,
            gridview: true,
            caption: 'Products'
        })
        .navGrid(pager, { edit: false, add: false, del: false, search: false, refresh: false });
        search.filterGrid("#" + grid.attr("id"), {
            gridModel: false,
            filterModel: [{
                label: 'Search',
                name: 'search',
                stype: 'text'
}]
            });
        }
    };
    function checksave(result) {
        if (result == "") {
            alert("Update is missing!");
            return false;
        }
        return true;
    }
    $(document).ready(function() {
        
    });

